package com.chengdu.his.care.handler;

import com.chengdu.his.care.service.PatientsDetailService;
import com.chengdu.his.care.service.PatientsService;
import com.chengdu.his.pojo.*;
import com.chengdu.his.utils.CommonResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/patients")
public class PatientsHandler {

    @Resource
    private PatientsService patientsService;

    @Resource
    private PatientsDetailService patientsDetailService;

    /**
     * 添加患者信息
     * @param patientDto 患者信息和其就诊信息的集合
     * @return CommonResult 统一返回类
     */
    @PostMapping("/add")
    public CommonResult addPatients(@RequestBody PatientDto patientDto) {
        // 添加基础信息
        try {
            boolean added = patientsService.addPatients(patientDto.getPatients(), patientDto.getPatientsDetail());
            return added ? CommonResult.success(patientDto) : CommonResult.error(201, "添加患者信息失败");
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.error(500, "系统异常,添加失败,请联系管理员.");
        }

    }

    @GetMapping("/{pageNum}/{pageSize}")
    public CommonResult<PageInfo<Patients>> listPatients(@PathVariable("pageNum") int pageNum, @PathVariable("pageSize") int pageSize) {
        try {
            PageHelper.startPage(pageNum, pageSize); // 分页查询只需要这一行，不用在xml中配置，底层用的是拦截器
            // PageHelper.offsetPage(pageNum, pageSize);
            // return dictService.dicts().size() > 0 ? CommonResult.success(dictService.dicts()) : CommonResult.error(401, "请求成功,但没有对应的数据.");
            // 优化分页
            List<Patients> patients = patientsService.listPatientsByPage();
            PageInfo<Patients> pageInfo = new PageInfo<>(patients);
            return patients.size() > 0 ? CommonResult.success(pageInfo) : CommonResult.error(401, "请求成功,但没有对应数据.");
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.error(503, "系统异常,查询失败,请联系管理员.");
        }
    }
    /**
     * 根据id查询患者详细信息
     * @param val 患者id
     * @return CommonResult 统一返回类
     */
    @GetMapping("/detail/{val}")
    public CommonResult<PatientsDetail> findPatientsDetailBypId(@PathVariable("val") int val) {
        try {
            PatientsDetail patientsDetail = patientsDetailService.findById(val);
            return null != patientsDetail ? CommonResult.success(patientsDetail) : CommonResult.error(201, "请求成功,没有查询到患者数据.");
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.error(500, "系统异常,获取患者列表数据失败,请联系管理员.");
        }
    }

    /**
     * 根据id查询患者基本信息
     * @param pid 患者id
     * @return CommonResult 统一返回类
     */
    @GetMapping("/{pid}")
    public CommonResult<Patients> findPatientsBypId(@PathVariable("pid") int pid) {
        try {
            Patients patients = patientsService.findById(pid);
            return null != patients ? CommonResult.success(patients) : CommonResult.error(201, "请求成功,没有查询到患者数据.");
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.error(500, "系统异常,获取患者列表数据失败,请联系管理员.");
        }
    }

    /**
     * 添加患者筛查信息
     * @param screening 患者检查信息
     * @return CommonResult 统一返回类
     */
    @PostMapping("/screening")
    public CommonResult insertScreening(@RequestBody Screening screening) {
        try {
            int i = patientsService.insertScreening(screening);
            return i > 0 ? CommonResult.success(screening) : CommonResult.error(201, "请求成功,添加失败,请重试.");
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.error(500, "系统异常,添加失败,请联系管理员.");
        }
    }


}
